package org.vacoor.nothing.web.util;

import com.google.common.base.Charsets;
import com.google.common.collect.Maps;
import org.vacoor.nothing.common.codec.Base64;
import org.vacoor.nothing.common.codec.Hex;
import org.vacoor.nothing.common.crypto.digest.Hash;
import org.vacoor.nothing.common.util.Bytes;
import org.vacoor.nothing.common.util.HttpUtils;
import org.vacoor.nothing.common.util.IOUtils;

import java.io.IOException;
import java.util.UUID;

/**
 */
public abstract class Tomcats {
    private static final String LIST = "list";
    private static final String START = "start";
    private static final String STOP = "stop";
    private static final String RELOAD = "reload";
    private static final String UNDEPLOY = "undeploy";

    public static String list(String serverUrl) throws IOException {
        return IOUtils.readText(HttpUtils.get(serverUrl + "/manager/text/list", null, 10 * 1000), Charsets.UTF_8, true);
    }

    public static String start(String serverUrl, String contextPath) throws IOException {
        return doInternal(serverUrl, contextPath, START);
    }

    public static String stop(String serverUrl, String contextPath) throws IOException {
        return doInternal(serverUrl, contextPath, STOP);
    }

    // http://{username}:{password}@{host}/manager/text/{command}?path=/
    // http://tomcat.apache.org/tomcat-7.0-doc/manager-howto.html#Supported_Manager_Commands
    private static String doInternal(String serverUrl, String contextPath, String command) throws IOException {
        String commandUrl = serverUrl + "/manager/text/" + command + "?path=" + contextPath;
        return IOUtils.readText(HttpUtils.get(commandUrl, null, 10 * 1000), Charsets.UTF_8, true);
    }
}
